package com.point.business.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Dur;
import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Description;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.UidGenerator;

import org.apache.log4j.Logger;

import com.point.business.database.managers.InformationManager;
import com.point.business.database.model.InformationSL;

/**
 * Servlet implementation class LoginServlet
 */

public class CalendarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(CalendarServlet.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CalendarServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Content-Disposition",
				"inline;filename=\"event.ics\"");
		response.setContentType("text/calendar");
		int id = Integer.valueOf(request.getParameter("id"));
		InformationSL informationSL = InformationManager.getInstance()
				.getInformationSLById(id);
		java.util.Date datum = getZakazanDatum(informationSL.getDesctription());
		Calendar calendar = new Calendar();
		calendar.getProperties().add(
				new ProdId("-//Ilija Trajkoski//iCal4j 1.0//EN"));
		calendar.getProperties().add(Version.VERSION_2_0);
		calendar.getProperties().add(CalScale.GREGORIAN);
		VEvent theEvent = new VEvent(new DateTime(datum.getTime()), new Dur(0,
				2, 0, 0), informationSL.getTitle());

		UidGenerator ug = new UidGenerator("1");
		theEvent.getProperties().add(ug.generateUid());
		theEvent.getProperties().add(
				new Description(informationSL.getDesctription()));
		calendar.getComponents().add(theEvent);
		ServletOutputStream fout = response.getOutputStream();
		CalendarOutputter outputter = new CalendarOutputter();
		try {
			outputter.output(calendar, fout);
		} catch (ValidationException e) {
			logger.error(e);
		}
		fout.flush();

	}

	private java.util.Date getZakazanDatum(String description) {
		java.util.Date result = new java.util.Date();
		//se zakazuva
		//if (description.contains("�� ��������")) {
		// if (false) {
		// //na den
		// int startPossDate = description.indexOf("�� ��� ");
		// String date = "";
		// if (startPossDate > -1) {
		// date = description.substring(startPossDate + 7,
		// startPossDate + 17);
		// }
		// //godina vo
		// int startPossTime =
		// description.indexOf(" ������ �� ");
		// String time = "";
		// if (startPossTime > -1) {
		// time = description.substring(startPossTime + 11,
		// startPossTime + 17);
		// }
		// if (!"".equals(date) && !"".equals(time)) {
		// SimpleDateFormat formatter = new SimpleDateFormat(
		// "dd.MM.yyyy-HH,mm");
		// try {
		// result = formatter.parse(date + "-" + time);
		// } catch (ParseException e) {
		// logger.error(e);
		// }
		// }
		// }
		return result;
	}
}
